package com.hxkj.cst.chengshuitong.ui.activity

import android.content.Intent
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.support.v7.app.ActionBarDrawerToggle
import android.view.Gravity
import android.view.KeyEvent
import android.view.MenuItem
import com.hxkj.cst.chengshuitong.R
import com.hxkj.cst.chengshuitong.base.BaseActivity
import com.hxkj.cst.chengshuitong.base.BasePresenter
import com.hxkj.cst.chengshuitong.toast
import com.hxkj.cst.chengshuitong.ui.adapter.MainViewPagerAdapter
import com.hxkj.cst.chengshuitong.ui.fragment.ChoseFragment
import com.hxkj.cst.chengshuitong.ui.fragment.HomeFragment
import com.hxkj.cst.chengshuitong.ui.fragment.OrderFragment
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.include_toolbar.*
import java.util.*
import kotlin.collections.ArrayList

/**
 * 作者： Luoyang on 2017/5/25.
 * 描述：
 */
class MainActivity : BaseActivity<BasePresenter>() {
    val titleResource = ArrayList<String>()
    val iconResource = ArrayList<Int>()

    override fun provideContentView(): Int = R.layout.activity_main

    override fun onCreate(savedInstanceState: android.os.Bundle?) {
        super.onCreate(savedInstanceState)

        supportActionBar?.setHomeAsUpIndicator(R.mipmap.ic_menu_white_36dp)
        setToolbarTitle(getString(R.string.title_home))

        setUpDrawer()
        setUpViewPager()
        setUpTabLayout()
    }

    private fun setUpDrawer() {
        val toggle = ActionBarDrawerToggle(this, am_drawer, toolbar, R.string.state_drawer_open, R.string.state_drawer_close)
        am_drawer.addDrawerListener(toggle)
        toggle.syncState()

        am_navigation_view.setNavigationItemSelectedListener { menuItem: MenuItem ->
            am_drawer.closeDrawer(Gravity.START)
            when (menuItem.itemId) {
                R.id.drawer_car_manager -> toast(R.string.hint_open_soon)
                R.id.drawer_message_center -> toast(R.string.hint_open_soon)
                R.id.drawer_violation -> toast(R.string.hint_open_soon)
                R.id.drawer_setting_center -> toast(R.string.hint_open_soon)
            }
            true
        }

        am_navigation_view.getHeaderView(0).setOnClickListener({
            startActivity(Intent(this, LoginActivity::class.java))
        })
    }

    private fun setUpViewPager() {
        val fragmentArray: ArrayList<Fragment> =
                arrayListOf(HomeFragment(), OrderFragment(), ChoseFragment())
        val viewPagerAdapter = MainViewPagerAdapter(supportFragmentManager, fragmentArray)
        am_viewpager.adapter = viewPagerAdapter
        am_viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(p0: Int) {
            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
            }

            override fun onPageSelected(p0: Int) {
                setToolbarTitle(titleResource[p0])
            }
        })
    }

    private fun setUpTabLayout() {
        am_tab_layout.setupWithViewPager(am_viewpager)
        am_tab_layout.tabMode = TabLayout.MODE_FIXED

        initTabLayoutResource()

        for (index in 0 until titleResource.size) {
            val tabItem = am_tab_layout.getTabAt(index)
            tabItem?.setIcon(iconResource[index])
            tabItem?.setText(titleResource[index])
        }
    }

    private fun initTabLayoutResource() {
        var typeArray = resources.obtainTypedArray(R.array.Navigation_Title)
        for (index in 0 until typeArray.length()) {
            titleResource.add(index, typeArray.getString(index))
        }
        typeArray = resources.obtainTypedArray(R.array.Navigation_Icon)
        for (index in 0 until typeArray.length()) {
            iconResource.add(index, typeArray.getResourceId(index, R.mipmap.home))
        }
        typeArray.recycle()
    }

    override fun onSupportNavigateUp(): Boolean = false

    var lastClickTime: Long = 0
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (am_drawer.isDrawerOpen(Gravity.START))
                am_drawer.closeDrawer(Gravity.START) else {
                val currentClickTime = Date().time
                if (currentClickTime - lastClickTime < 2000) {
                    System.exit(0)
                    return true
                } else {
                    toast(R.string.hint_event_back)
                    lastClickTime = currentClickTime
                }
            }
        }
        return false
    }
}